package models

import (
	"time"

	"gorm.io/gorm"
)

// Sample 送样品
type Sample struct {
	SampleId int `gorm:"primary_key" json:"sample_id"` // 样品ID

	UserId        int    `json:"user_id"`        // 创建人
	GoodsId       int    `json:"goods_id"`       // 商品Id
	GoodsNum      int    `json:"goods_num"`      // 商品数量
	StreamerId    int    `json:"streamer_id"`    // 主播ID
	ExpressName   string `json:"express_name"`   // 快递名称
	ExpressNo     string `json:"express_no"`     // 快递单号
	InstitutionId int    `json:"institution_id"` // 机构ID

	CreateTime time.Time `json:"create_time"` // 创建时间
	UpdateTime time.Time `json:"update_time"` // 更新时间

	Streamer Streamer
	User     User
	Goods    Goods
}

// TableName 表名称
func (*Sample) TableName() string {
	return "sample"
}

// GetSample 查询多条数据
func GetSample(fields []string, maps map[string]interface{}) ([]*Sample, error) {
	var address []*Sample
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&address).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return address, nil
}

func GetSamplePage(fields []string, maps map[string]interface{}, pageNum int, pageSize int, order string) ([]*Sample, error) {
	var Sample []*Sample
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Preload("Streamer").Preload("User").Preload("Goods").Where(cond, vals...).Offset(pageNum).Limit(pageSize).Order(order).Find(&Sample).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return Sample, nil
}

// GetSampleInfo 查询多条数据
func GetSampleInfo(fields []string, maps map[string]interface{}) (*Sample, error) {
	var Sample Sample
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&Sample).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return &Sample, nil
}

// AddSample 插入数据
func AddSample(data map[string]interface{}) (int, error) {
	address := Sample{
		UserId:        data["user_id"].(int),
		GoodsId:       data["goods_id"].(int),
		GoodsNum:      data["goods_num"].(int),
		StreamerId:    data["streamer_id"].(int),
		ExpressName:   data["express_name"].(string),
		ExpressNo:     data["express_no"].(string),
		InstitutionId: data["institution_id"].(int),
	}
	result := db.Select([]string{"user_id", "goods_id", "goods_num", "streamer_id", "express_name", "express_no", "institution_id"}).Create(&address)
	if err := result.Error; err != nil {
		return 0, err
	}
	return address.SampleId, nil
}

// EditSample modify a single Sample
func EditSample(where map[string]interface{}, data interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Model(&Sample{}).Where(cond, vals...).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

func GetSampleTotal(where map[string]interface{}) (int64, error) {
	var count int64
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return 0, errs
	}
	if err := db.Model(&Sample{}).Where(cond, vals...).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func CleanSample(where map[string]interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Unscoped().Where(cond, vals...).Delete(&Sample{}).Error; err != nil {
		return err
	}

	return nil
}
